package com.ben.multiple.poi.rest;

import com.ben.multiple.poi.PoiUtils;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author BenJin Yin
 * @date 2021/1/7
 */
@RestController
@RequestMapping("/export")
public class ExportRest {
    static final String[] EXPORT_HEADER = {
            "提单号", "箱号", "空重", "尺寸", "类型",
            "箱公司", "铅封号", "内外贸", "条款", "货名",
            "件数", "毛重", "体积", "特种箱", "装货港",
            "目的港", "卸货港"};
    static final String COMMON_FILL = "0";

    @GetMapping
    public void export(HttpServletResponse response) {
        List<List<String>> data = this.getData();
        Workbook workbook = PoiUtils.generateWorkbook(data, EXPORT_HEADER);
        PoiUtils.responseWorkbook(response, workbook);
    }

    List<List<String>> getData() {
        final int num = 1000;
        int fill = String.valueOf(num).length() - 1;
        final String billNo = "BYC202101071";
        final String containerNoPrefix = "BYCA21";
        List<List<String>> data = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            List<String> row = new ArrayList<>();
            row.add(billNo);
            row.add(containerNoPrefix + this.getSequenceString(i, fill));
            row.add("F");
            row.add("20");
            row.add("GP");

            row.add("CONS");
            row.add("YC-921" + this.getSequenceString(i, fill));
            row.add("内贸");
            row.add("");
            row.add("块煤");

            row.add("");
            row.add("30000");
            row.add("");
            row.add("");
            row.add("天津新港（区）");

            row.add("黄石新港");
            row.add("黄石新港");
            data.add(row);
        }
        return data;
    }

    String getSequenceString(int num, int bit) {
        StringBuilder value = new StringBuilder(String.valueOf(num));
        int length = value.length();
        for (int i = length; i < bit; i++) {
            value.insert(0, COMMON_FILL);
        }
        return value.toString();
    }
}
